Imports System.Data.SqlClient

Friend Class DBField
    Inherits DBBase

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub Load(ByRef oObject As SQLAutoDocLib.Field)
        Dim sSQL As String = "select"
        sSQL = sSQL & "     [TableID],"
        sSQL = sSQL & "     [TableRevID],"
        sSQL = sSQL & "     [Name],"
        sSQL = sSQL & "     [Desc],"
        sSQL = sSQL & "     [Type],"
        sSQL = sSQL & "     [Size],"
        sSQL = sSQL & "     [Scale],"
        sSQL = sSQL & "     [Precision],"
        sSQL = sSQL & "     [ReferencesFK],"
        sSQL = sSQL & "     [IsNotNull],"
        sSQL = sSQL & "     [IsIdentity],"
        sSQL = sSQL & "     [DefaultValue],"
        sSQL = sSQL & "     [Constraint],"
        sSQL = sSQL & "     [Order],"

        sSQL = sSQL & "     [CreateName],"
        sSQL = sSQL & "     [CreateDate],"
        sSQL = sSQL & "     [ChangeName],"
        sSQL = sSQL & "     [ChangeDate]"
        sSQL = sSQL & " from"
        sSQL = sSQL & "     tDDFields"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        Dim oData As DataTable = MyBase.ExecuteReadToDataTable(sSQL)

        With oObject
            .TableID = oData.Rows(0).Item("TableID")
            .TableRevID = oData.Rows(0).Item("TableRevID")
            .Name = oData.Rows(0).Item("Name")
            .Desc = NullToString(oData.Rows(0).Item("Desc"))
            .Type = oData.Rows(0).Item("Type")
            .Size = oData.Rows(0).Item("Size")
            .Scale = NullToNumber(oData.Rows(0).Item("Scale"))
            .Precision = NullToNumber(oData.Rows(0).Item("Precision"))
            .ReferencesFK = oData.Rows(0).Item("ReferencesFK")
            .IsNotNull = oData.Rows(0).Item("IsNotNull")
            .IsIdentity = oData.Rows(0).Item("IsIdentity")
            .DefaultValue = NullToString(oData.Rows(0).Item("DefaultValue"))
            .Constraint = NullToString(oData.Rows(0).Item("Constraint"))
            .Order = oData.Rows(0).Item("Order")

            .CreateName = oData.Rows(0).Item("CreateName")
            .CreateDate = oData.Rows(0).Item("CreateDate")
            .ChangeName = NullToString(oData.Rows(0).Item("ChangeName"))
            .ChangeDate = NullToString(oData.Rows(0).Item("ChangeDate"))
        End With

        oData.Dispose()
    End Sub

    Public Sub Update(ByRef oObject As SQLAutoDocLib.Field)

        Dim sSQL As String = "update tDDFields set"
        sSQL = sSQL & "     [TableID]=" & ValueToDBValue(oObject.TableID) & ","
        sSQL = sSQL & "     [TableRevID]=" & ValueToDBValue(oObject.TableRevID) & ","
        sSQL = sSQL & "     [Name]=" & ValueToDBValue(oObject.Name) & ","
        sSQL = sSQL & "     [Desc]=" & ValueToDBValue(oObject.Desc) & ","
        sSQL = sSQL & "     [Type]=" & ValueToDBValue(oObject.Type) & ","
        sSQL = sSQL & "     [Size]=" & ValueToDBValue(oObject.Size) & ","
        sSQL = sSQL & "     [Scale]=" & ValueToDBValue(oObject.Scale) & ","
        sSQL = sSQL & "     [Precision]=" & ValueToDBValue(oObject.Precision) & ","
        sSQL = sSQL & "     [ReferencesFK]=" & BooleanToBit(oObject.ReferencesFK) & ","
        sSQL = sSQL & "     [IsNotNull]=" & BooleanToBit(oObject.IsNotNull) & ","
        sSQL = sSQL & "     [IsIdentity]=" & BooleanToBit(oObject.IsIdentity) & ","
        sSQL = sSQL & "     [DefaultValue]=" & ValueToDBValue(oObject.DefaultValue) & ","
        sSQL = sSQL & "     [Constraint]=" & ValueToDBValue(oObject.Constraint) & ","
        sSQL = sSQL & "     [Order]=" & ValueToDBValue(oObject.Order) & ","

        sSQL = sSQL & "     [CreateName]=" & ValueToDBValue(oObject.CreateName) & ","
        sSQL = sSQL & "     [CreateDate]=" & ValueToDBValue(oObject.CreateDate) & ","
        sSQL = sSQL & "     [ChangeName]=" & ValueToDBValue(oObject.ChangeName) & ","
        sSQL = sSQL & "     [ChangeDate]=" & ValueToDBValue(oObject.ChangeDate)
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Add(ByRef oObject As SQLAutoDocLib.Field)
        Dim oNextID As New DBNextIDValue("Field")
        Dim lNextID As Long = oNextID.NextValue
        oObject.ID = lNextID

        Dim sSQL As String = "insert into tDDFields ("
        sSQL = sSQL & "     [ID],"
        sSQL = sSQL & "     [TableID],"
        sSQL = sSQL & "     [TableRevID],"
        sSQL = sSQL & "     [Name],"
        sSQL = sSQL & "     [Desc],"
        sSQL = sSQL & "     [Type],"
        sSQL = sSQL & "     [Size],"
        sSQL = sSQL & "     [Scale],"
        sSQL = sSQL & "     [Precision],"
        sSQL = sSQL & "     [ReferencesFK],"
        sSQL = sSQL & "     [IsNotNull],"
        sSQL = sSQL & "     [IsIdentity],"
        sSQL = sSQL & "     [DefaultValue],"
        sSQL = sSQL & "     [Constraint],"
        sSQL = sSQL & "     [Order],"

        sSQL = sSQL & "     [CreateName],"
        sSQL = sSQL & "     [CreateDate],"
        sSQL = sSQL & "     [ChangeName],"
        sSQL = sSQL & "     [ChangeDate]) values ("

        sSQL = sSQL & ValueToDBValue(oObject.ID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.TableID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.TableRevID) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Name) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Desc, 2500) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Type) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Size) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Scale) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Precision) & ","
        sSQL = sSQL & BooleanToBit(oObject.ReferencesFK) & ","
        sSQL = sSQL & BooleanToBit(oObject.IsNotNull) & ","
        sSQL = sSQL & BooleanToBit(oObject.IsIdentity) & ","
        sSQL = sSQL & ValueToDBValue(oObject.DefaultValue, 255) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Constraint, 255) & ","
        sSQL = sSQL & ValueToDBValue(oObject.Order) & ","

        sSQL = sSQL & ValueToDBValue(oObject.CreateName) & ","
        sSQL = sSQL & ValueToDBValue(oObject.CreateDate) & ","
        sSQL = sSQL & ValueToDBValue(oObject.ChangeName) & ","
        sSQL = sSQL & ValueToDBValue(oObject.ChangeDate) & ")"

        MyBase.ExecuteNonQuery(sSQL)
    End Sub

    Public Sub Delete(ByRef oObject As SQLAutoDocLib.Field)
        Dim sSQL As String = "delete from tDDFields"
        sSQL = sSQL & " where"
        sSQL = sSQL & "     [ID]=" & oObject.ID

        MyBase.ExecuteNonQuery(sSQL)
    End Sub
End Class
